package com.example.administrator.nurse.fragment;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.nurse.R;
import com.example.administrator.nurse.base.BaseFragment;
import com.example.administrator.nurse.model.CommonViewHolder;
import com.example.administrator.nurse.model.RecyclerViewCommonAdapter;
import com.example.administrator.nurse.model.bean.PatientRoomBean;
import com.example.administrator.nurse.present.PatientInfoActivity;
import com.example.administrator.nurse.present.PatientRoomActivity;
import com.example.administrator.nurse.util.recyclerViewHelper;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.jcodecraeer.xrecyclerview.progressindicator.DivierItemDecoration;

import java.util.List;

/**
 * Created by Administrator on 2016/4/26.
 *
 */

//病房巡视--按病人   这里的implements 主Activity 为了数据回调？？
public class PatientAtRoomFragment extends BaseFragment implements PatientRoomActivity.OnDealWithDataListener {

    public RecyclerViewCommonAdapter adapter;
    private recyclerViewHelper recyclerViewHelper;
    private int[] flagList={R.mipmap.red_flag, R.mipmap.orange_flag, R.mipmap.yellow_flag, R.mipmap.green_flag};

    @Override
    protected void initView(View rootView){

        initAdapter();
        recyclerViewHelper=new recyclerViewHelper(activity,(XRecyclerView)f(R.id.recyclerView))
                .withAdapter(adapter)
                .withLayoutManager(new LinearLayoutManager(activity))
                .withPullRefrshOpen(true)
                .withRefresh(new recyclerViewHelper.refreshListener() {
                    @Override
                    public void refresh() {
                        PatientRoomActivity.PatientGetDataListener.onGetData(false);
                    }
                })
                .withLoadMore(new recyclerViewHelper.loadMoreListener() {
                    @Override
                    public void loadMore() {
                        PatientRoomActivity.PatientGetDataListener.onGetData(true);
                    }
                })
                .initRecyclerView();
        recyclerViewHelper.addItemDecoration(DivierItemDecoration.VERTICAL_LIST);
    }

    @Override
    public int setLayoutId() {
        return R.layout.patient_at_room_fragment;
    }

    @Override
    protected void onFailure(int requestCode, Object object, String errorMsg) {

    }

    @Override
    protected void onSuccess(int requestCode, Object object, String response) {

    }



    private void initAdapter(){
//        for (int i=0;i<20;i++){
//            PatientRoomBean bean=new PatientRoomBean();
//            bean.setName("王小二");
//            bean.setAge("68岁");
//            bean.setSex("女");
//            bean.setAreanum("2");
//            bean.setRoomnum("5");
//            bean.setDate("2016-3-23");
//            bean.setTime("12:11:23");
//            bean.setState(PatientRoomBean.SPECIFY);
//            list.add(bean);
//        }
        adapter=new RecyclerViewCommonAdapter(getActivity(),PatientRoomActivity.list) {

            @Override
            protected int getLayoutId() {
                Log.d("zzzzz"," adapter init ");
                return R.layout.patient_list_item;
            }

            @Override
            protected void bindView(CommonViewHolder holder, final int position) {

                LinearLayout beanfather;
                TextView name,room_area_num,date;
                ImageView flag;
                PatientRoomBean bean = PatientRoomActivity.list.get(position);

                name=holder.getTextView(R.id.name);
                room_area_num=holder.getTextView(R.id.area_room);
                date=holder.getTextView(R.id.tour_time);
                flag=holder.getImageView(R.id.nurse_level_flag);

                name.setText(bean.getName()+","+bean.getSex()+","+bean.getAge());
                room_area_num.setText(bean.getAreanum()+";"+bean.getRoomnum());
                date.setText("最后巡视时间："+bean.getTime());
                flag.setImageResource(flagList[bean.getLevel()]);

                beanfather=holder.getLinearLayout(R.id.bean_father);
                beanfather.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onItemClickListener.onItemClick(null,position);
                    }
                });
            }
        };
        adapter.setOnItemClickListener(new RecyclerViewCommonAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {

                Intent intent = new Intent(getActivity(),PatientInfoActivity.class);
                intent.putExtra("data",PatientRoomActivity.list.get(position));
                startActivityForResult(intent, PatientRoomActivity.OnSaveCallBack);

            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });
    }

    @Override
    public void onSuccessDealWith(int requestCode, Object object, String response) {
        List<String> l = (List<String>) object;
        Log.d("zzzzz", l.get(0));
        try{
            PatientRoomActivity.SearchInput.type.setText(l.get(0));
            PatientRoomActivity.SearchInput.sort.setText(l.get(1));
            PatientRoomActivity.dealDataJson(response, false);
            switch(requestCode){
                case PatientRoomActivity.GetDataNormal:
                    recyclerViewHelper.getRecyclerView().refreshComplete();
                    if(response.trim().equals("0")){
                        t("暂无记录!");
                        PatientRoomActivity.list.clear();
                        adapter.notifyDataSetChanged();
                        //userTestData(); /** 数据库没数据，使用测试数据，日后可以删除 */
                    }else {
                        t(response);
                        PatientRoomActivity.dealDataJson(response, false);
                        adapter.notifyDataSetChanged();
                    }
                    break;
                case PatientRoomActivity.GetDataMore:
                    recyclerViewHelper.getRecyclerView().loadMoreComplete();
                    PatientRoomActivity.dealDataJson(response, true);
                    adapter.notifyDataSetChanged();
                    break;
            }
        }catch (Exception ignored){}
    }

    @Override
    public void onFailureDealWith(int requestCode, Object object, String response) {

    }
}
